﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web.Mvc;
using System.Text;
using System.EnterpriseServices;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model.ParamModel;
using YiYiTong.Rocket.EFDBFirst;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.ManageWeb.Helper;
using YiYiTong.Rocket.Business.Biz;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class productController : BaseController
    {
        #region 获取列表
        [Description("在售物料列表")]
        [ViewPage]
        public ActionResult productlist(ProductListQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;
            var queryable = getProductList(queryModel);

            int rowcounts = queryable.Count();
            var querylist = queryable.OrderByDescending(t => t.with_up_time).ThenByDescending(t => t.goods_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();


            ViewData["AllCount"] = rowcounts;

            PagedList<Views_Goods> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<Views_Goods>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }
        private IQueryable<Views_Goods> getProductList(ProductListQueryModel queryModel)
        {

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            var queryable = dbcontext.Views_Goods.Where(t => t.is_del == 0);




            if (queryModel.goods_id.HasValue && queryModel.goods_id.Value > 0)
            {
                queryable = queryable.Where(t => t.goods_id == queryModel.goods_id.Value);
            }
            if (!string.IsNullOrEmpty(queryModel.draw_down_reason))
            {
                queryable = queryable.Where(t => t.draw_down_reason.Contains(queryModel.draw_down_reason));
            }


            if (queryModel.label_type != -10)
                queryable = queryable.Where(t => t.label_type == queryModel.label_type);

            if (queryModel.category_id != -10)
            {
                var category = dbcontext.ADZ_GoodsCategory.FirstOrDefault(t => t.category_id == queryModel.category_id);
                if (category != null)
                {
                    var categorylist = dbcontext.ADZ_GoodsCategory.Where(t => t.category_code.StartsWith(category.category_code)).ToList();
                    if (categorylist.Any())
                    {
                        List<long?> category_id_list = new List<long?>();
                        categorylist.ForEach(t => { category_id_list.Add(t.category_id); });
                        queryable = queryable.Where(t => category_id_list.Contains(t.category_id));
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(queryModel.brand_id_name))
            {
                var brandmaster = dbcontext.ADZ_BrandMaster.FirstOrDefault(t => t.brand_name == queryModel.brand_id_name);
                if (brandmaster == null)
                {
                    return null;
                }
                queryModel.brand_id = brandmaster.brand_id;
            }
            if (queryModel.brand_id > 0)
            {
                queryable = queryable.Where(t => t.brand_id == queryModel.brand_id);
            }

            if (!string.IsNullOrWhiteSpace(queryModel.supplier_id_name))
            {
                var item = dbcontext.ADZ_SupplierMaster.FirstOrDefault(t => t.supplier_name == queryModel.supplier_id_name);
                if (item == null)
                {
                    return null;
                }
                queryModel.supplier_id = item.supplier_id;
            }
            if (queryModel.supplier_id > 0)
            {
                queryable = queryable.Where(t => t.supplier_id == queryModel.supplier_id);
            }

            if (queryModel.supplier_id != -10)
                queryable = queryable.Where(t => t.supplier_id == queryModel.supplier_id);

            if (queryModel.is_withdraw != -10)
                queryable = queryable.Where(t => t.is_withdraw == queryModel.is_withdraw);
            if (queryModel.stock_amount != -10)
            {
                if (queryModel.stock_amount == 2)
                {
                    queryable = queryable.Where(t => t.stock_amount >= t.min_amount);
                }
                else if (queryModel.stock_amount == 1)
                {
                    queryable = queryable.Where(t => t.stock_amount < t.min_amount || (!(t.stock_amount > 0)));
                }
                else
                {
                    queryable = queryable.Where(t => !(t.stock_amount > 0));
                }
            }

            if (!string.IsNullOrWhiteSpace(queryModel.MPN))
                queryable = queryable.Where(t => t.MPN.StartsWith(queryModel.MPN));

            if (queryModel.end_time_begin.HasValue)
                queryable = queryable.Where(t => t.end_time >= queryModel.end_time_begin);
            if (queryModel.end_time_end.HasValue)
            {
                var endTime = queryModel.end_time_end.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.end_time < endTime);
            }

            if (queryModel.with_up_time_begin.HasValue)
                queryable = queryable.Where(t => t.with_up_time >= queryModel.with_up_time_begin);
            if (queryModel.with_up_time_end.HasValue)
            {
                var endTime = queryModel.with_up_time_end.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.with_up_time < endTime);
            }

            if (queryModel.with_down_time_begin.HasValue)
                queryable = queryable.Where(t => t.with_down_time >= queryModel.with_down_time_begin);
            if (queryModel.with_down_time_end.HasValue)
            {
                var endTime = queryModel.with_down_time_end.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.with_down_time < endTime);
            }

            return queryable;
        }

        [NoVerify]
        public ActionResult downProductlist(ProductListQueryModel queryModel)
        {
            queryModel.Trim();
            var queryable = getProductList(queryModel);

            int rowcounts = queryable.Count();
            var querylist = queryable.OrderByDescending(t => t.update_time).ThenByDescending(t => t.goods_id)
                .ToList();


            string strOutPut = string.Empty;
            string Excel = ProductlistExcel(querylist, out strOutPut);
            ResponseMsg resMsg = new ResponseMsg();

            if (strOutPut != "导出成功")
            {
                resMsg.IsSuccess = false;
                resMsg.Message = strOutPut;

                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            //第一种:使用FileContentResult
            byte[] fileContents = Encoding.UTF8.GetBytes(Excel.ToString());
            return File(fileContents, "application/ms-excel", "productlist_" + DateTime.Now.ToString(4) + ".xls");
        }


        public string ProductlistExcel(List<Views_Goods> listmodel, out string strOutPut)
        {
            List<string> list_title = new List<string> {
                "料号",
                "品牌",
                "分类",
                "供应商",
                "板块",
                "普通会员可看",
                "结束时间",
                "单位",
                "DC号",
                "库存数量",
                "最小订购量",
                "价格",
                "币种",
                "市场价",
                "是否含税",
                "上架时间",
                "是否上架",
                "描述"
            };
            ExportExcel Excel = new ExportExcel();
            DataTable dt = new DataTable();
            list_title.ForEach(t => dt.Columns.Add(t));
            if (!listmodel.Any())
            {
                return Excel.DtToExcel(dt, list_title, out strOutPut);
            }

            var labelDicTypeName = ReleaseBiz.GetLabelTypeNameDic();
            for (int i = 0; i < listmodel.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["料号"] = listmodel[i].MPN;
                dr["品牌"] = listmodel[i].brand_name;
                dr["分类"] = listmodel[i].category_name;
                dr["供应商"] = listmodel[i].supplier_name;
                dr["板块"] = labelDicTypeName[listmodel[i].label_type.Value.ToString()];
                dr["普通会员可看"] = listmodel[i].show_min_level == 0 ? "可看" : "不可看";
                dr["结束时间"] = listmodel[i].end_time.ToString(1);
                dr["单位"] = listmodel[i].goods_unit;
                dr["DC号"] = listmodel[i].DCN;
                dr["库存数量"] = listmodel[i].stock_amount;
                dr["最小订购量"] = listmodel[i].min_amount;//
                dr["价格"] = listmodel[i].price;//
                dr["币种"] = listmodel[i].currency_type == 0 ? "人民币" : "美元";//
                dr["市场价"] = listmodel[i].market_price;//
                dr["是否含税"] = listmodel[i].include_tax == 0 ? "未含税" : "已含税";//
                dr["上架时间"] = listmodel[i].with_up_time.ToString(1);//
                dr["是否上架"] = listmodel[i].is_withdraw == 0 ? "已上架" : "已下架";
                dr["描述"] = listmodel[i].product_desc;//

                dt.Rows.Add(dr);
            }

            return Excel.DtToExcel(dt, list_title, out strOutPut);
        }

        #endregion

        #region 供应商商品列表


        [Description("供应商商品列表")]
        [ViewPage]
        public ActionResult SupplierProducts(ProductListQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            //获取登录用户名
            string loginName = new CurrentUser().user.loginName;
            //获取是否代理商信息
            Entity.UserInfo managerUser = UserBiz.Instance.GetUserInfo(loginName);
            if (managerUser != null && managerUser.supplier_id > 0)
            {
                queryModel.supplier_id = managerUser.supplier_id;
            }
            else
                return Redirect("/Home/Problem/");

            var queryable = getProductList(queryModel);
            int rowcounts = queryable.Count();
            var querylist = queryable.OrderByDescending(t => t.update_time).ThenByDescending(t => t.goods_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();


            ViewData["AllCount"] = rowcounts;

            PagedList<Views_Goods> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<Views_Goods>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }

        #endregion

        [Description("在售物料上架")]
        [HttpPost]
        public ActionResult toWithdrawUp(long id = 0)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            var entity = dbcontext.ADZ_GoodsMaster.FirstOrDefault(t => t.goods_id == id);
            if (entity != null)
            {
                entity.is_withdraw = 0;
                entity.draw_down_reason = "";
                entity.with_up_time = DateTime.Now;
                dbcontext.SaveChanges();
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";

                BusinessMsgBiz.Instance.SetManagerMsgConfig(new CurrentUser().user.uid, 110);
            }

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [ViewPage]
        public ActionResult toWithdrawDown(long id = 0)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            Views_Goods paramModel = new Views_Goods();
            if (id > 0)
            {
                paramModel = dbcontext.Views_Goods.FirstOrDefault(t => t.goods_id == id && t.is_del == 0);

                ViewBag.Title = "在售物料下架";
            }
            else
                ViewBag.Title = "在售物料下架";

            return View(paramModel);
        }

        [Description("在售物料下架")]
        [HttpPost]
        public ActionResult toWithdrawDown(long id = 0, string reason = "")
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            if (string.IsNullOrEmpty(reason))
            {
                resMsg.Message = "请输入物料下架原因";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            var entity = dbcontext.ADZ_GoodsMaster.FirstOrDefault(t => t.goods_id == id);
            if (entity != null)
            {
                entity.with_down_time = DateTime.Now;
                entity.is_withdraw = 1;
                entity.draw_down_reason = reason;
                dbcontext.SaveChanges();
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("批量在售物料上下架")]
        [HttpPost]
        public ActionResult toWithdrawMore(string ids, byte withdraw, string reason = "")
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            if (string.IsNullOrWhiteSpace(ids))
            {
                resMsg.IsSuccess = false;
                resMsg.Message = $"传入的信息不对";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (ids.EndsWith(","))
                ids = ids.Substring(0, ids.Length - 1);
            StringBuilder strb = new StringBuilder();
            string[] id_arr = Regex.Split(ids, ",");
            foreach (var item in id_arr)
            {
                long num_id = ConvertHelper.TryPraseInt(item, 0);
                if (num_id > 0)
                {
                    var entity = dbcontext.ADZ_GoodsMaster.FirstOrDefault(t => t.goods_id == num_id);
                    if (entity != null)
                    {
                        entity.is_withdraw = withdraw;
                        if (withdraw == 1)//下架
                        {
                            entity.with_down_time = DateTime.Now;
                            entity.draw_down_reason = reason;
                        }
                        if (withdraw == 0)
                        {
                            entity.with_up_time = DateTime.Now;
                            entity.draw_down_reason = "";
                        }
                    }
                    else
                    {
                        strb.Append($"id[{num_id}]不存在，");
                    }
                }
            }

            dbcontext.SaveChanges();

            if (strb.Length == 0)
            {
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }
            else
            {
                resMsg.IsSuccess = false;
                resMsg.Message = strb.ToString();
            }
            BusinessMsgBiz.Instance.SetManagerMsgConfig(new CurrentUser().user.uid, 110);
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("设置普通会员可看")]
        [HttpPost]
        public ActionResult setshow_min_level(long id, byte show_min_level)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            var entity = dbcontext.ADZ_GoodsLabel.FirstOrDefault(t => t.goods_id == id);
            if (entity != null)
            {
                entity.show_min_level = show_min_level;
                dbcontext.SaveChanges();
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }


        [Description("显示详情")]
        [ViewPage]
        public ActionResult detailInfo(long id = 0)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            Views_Goods paramModel = new Views_Goods();
            if (id > 0)
            {
                paramModel = dbcontext.Views_Goods.FirstOrDefault(t => t.goods_id == id && t.is_del == 0);

                ViewBag.Title = "物料详情";
            }
            else
                ViewBag.Title = "物料详情";

            return View(paramModel);
        }


        [Description("修改在售物料信息")]
        [HttpPost]
        public ActionResult detailInfo(AddProductModel paramModel)
        {
            long goods_id = paramModel.views_Goods.goods_id;
            ResponseMsg resMsg = new ResponseMsg();
            if (!ModelState.IsValid)
            {
                resMsg.IsSuccess = false;
                resMsg.Message = ModelState.Values.First().Errors[0].ErrorMessage;
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            ViewBag.ParamModel = paramModel;

            //todo 逻辑错误
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var aDZ_GoodsMaster = dbcontext.ADZ_GoodsMaster.FirstOrDefault(t => t.goods_id == goods_id);

            Utils.Mapper(aDZ_GoodsMaster, paramModel.views_Goods, new List<string>());
            aDZ_GoodsMaster.update_time = DateTime.Now;

            var aDZ_GoodsLabel = dbcontext.ADZ_GoodsLabel.FirstOrDefault(t => t.goods_id == goods_id);

            #region 修改竞价和活动信息
            if (aDZ_GoodsLabel.label_type != paramModel.views_Goods.label_type)
            {
                //从竞价变成非竞价
                if (aDZ_GoodsLabel.label_type == (int)LabelType.Jingjia)
                {
                    var jingjiagoods = dbcontext.ADZ_GoodsJingjia.FirstOrDefault(t => t.goods_id == goods_id);
                    if (jingjiagoods != null)
                        dbcontext.ADZ_GoodsJingjia.Remove(jingjiagoods);
                }
                //从活动变成非活动
                if (aDZ_GoodsLabel.label_type == (int)LabelType.Huodong)
                {
                    var huodonggoods = dbcontext.ADZ_GoodsHuodong.FirstOrDefault(t => t.goods_id == goods_id);
                    if (huodonggoods != null)
                        dbcontext.ADZ_GoodsHuodong.Remove(huodonggoods);
                }
            }
            ///添加或修改竞价
            if (paramModel.views_Goods.label_type == (int)LabelType.Jingjia)
            {
                var jingjiagoods = dbcontext.ADZ_GoodsJingjia.FirstOrDefault(t => t.goods_id == goods_id);
                if (jingjiagoods == null)
                {
                    dbcontext.ADZ_GoodsJingjia.Add(new ADZ_GoodsJingjia
                    {
                        goods_id = goods_id,
                        jingjia_id = 0
                    });
                }
            }
            ///添加或修改活动
            if (paramModel.views_Goods.label_type == (int)LabelType.Huodong)
            {
                var huodonggoods = dbcontext.ADZ_GoodsHuodong.FirstOrDefault(t => t.goods_id == goods_id);
                if (huodonggoods == null)
                {
                    dbcontext.ADZ_GoodsHuodong.Add(new ADZ_GoodsHuodong
                    {
                        goods_id = goods_id,
                        huodong_id = 0
                    });
                }
            }
            #endregion 

            Utils.Mapper(aDZ_GoodsLabel, paramModel.views_Goods, new List<string>());



            //移除删掉的图片
            for (int i = paramModel.aDZ_GoodsIMGs.Count - 1; i >= 0; i--)
            {
                var item = paramModel.aDZ_GoodsIMGs[i];
                if (string.IsNullOrWhiteSpace(item.img_url))
                {
                    var img = dbcontext.ADZ_GoodsIMG.FirstOrDefault(t => t.seq_id == item.seq_id);
                    if (img != null)
                        dbcontext.ADZ_GoodsIMG.Remove(img);
                    paramModel.aDZ_GoodsIMGs.Remove(item);
                }
            }

            ///设置首图
            paramModel.aDZ_GoodsIMGs.ForEach(t =>
            {
                if (t.img_url == paramModel.first_img)
                    t.sort_NO = 1;
                else
                    t.sort_NO = 10;

                var img = dbcontext.ADZ_GoodsIMG.FirstOrDefault(a => a.seq_id == t.seq_id);
                if (img != null)
                {
                    img.sort_NO = t.sort_NO;
                }
            });

            ///以下添加新图片
            HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            if (hfc != null && hfc.Count > 0)
            {
                for (int i = 0; i < hfc.Count; i++)
                {
                    var file = hfc[i];
                    var putResult = ImageFileBiz.UpImg(file);
                    if (!putResult.Item1)
                    {
                        resMsg.Message += (putResult.Item2);
                    }
                    else
                    {
                        LogHelper.Info($"first_img {paramModel.first_img}");
                        LogHelper.Info($"file.FileName {file.FileName}");
                        var sort_NO = 10;
                        if (paramModel.first_img == file.FileName)
                        {
                            sort_NO = 1;
                        }
                        ADZ_GoodsIMG goodsImg = new ADZ_GoodsIMG()
                        {
                            goods_id = paramModel.views_Goods.goods_id,
                            create_time = DateTime.Now,
                            img_type = 1,
                            img_url = putResult.Item3,
                            sort_NO = sort_NO
                        };
                        dbcontext.ADZ_GoodsIMG.Add(goodsImg);
                    }
                }
            }

            dbcontext.SaveChanges();

            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("批量修改板块")]
        [HttpPost]
        public ActionResult ChangeLabelMore(string ids, byte label_type)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            if (string.IsNullOrWhiteSpace(ids))
            {
                resMsg.IsSuccess = false;
                resMsg.Message = $"传入的信息不对";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (ids.EndsWith(","))
                ids = ids.Substring(0, ids.Length - 1);
            StringBuilder strb = new StringBuilder();
            string[] id_arr = Regex.Split(ids, ",");
            foreach (var item in id_arr)
            {
                long num_id = ConvertHelper.TryPraseInt(item, 0);
                if (num_id > 0)
                {
                    var entity = dbcontext.ADZ_GoodsLabel.FirstOrDefault(t => t.goods_id == num_id);
                    if (entity != null)
                    {
                        entity.label_type = label_type;
                    }
                    else
                    {
                        strb.Append($"id[{num_id}]不存在，");
                    }
                }
            }

            dbcontext.SaveChanges();

            if (strb.Length == 0)
            {
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }
            else
            {
                resMsg.IsSuccess = false;
                resMsg.Message = strb.ToString();
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
        [Description("批量修改结束时间")]
        [HttpPost]
        public ActionResult ChangeEndtimeMore(string ids, DateTime? endtime)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            if (!endtime.HasValue)
            {
                resMsg.IsSuccess = false;
                resMsg.Message = $"结束时间有误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (string.IsNullOrWhiteSpace(ids))
            {
                resMsg.IsSuccess = false;
                resMsg.Message = $"传入的信息不对";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (ids.EndsWith(","))
                ids = ids.Substring(0, ids.Length - 1);
            StringBuilder strb = new StringBuilder();
            string[] id_arr = Regex.Split(ids, ",");
            foreach (var item in id_arr)
            {
                long num_id = ConvertHelper.TryPraseInt(item, 0);
                if (num_id > 0)
                {
                    var entity = dbcontext.ADZ_GoodsLabel.FirstOrDefault(t => t.goods_id == num_id);
                    if (entity != null)
                    {
                        entity.end_time = endtime;
                    }
                    else
                    {
                        strb.Append($"id[{num_id}]不存在，");
                    }
                }
            }

            dbcontext.SaveChanges();

            if (strb.Length == 0)
            {
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }
            else
            {
                resMsg.IsSuccess = false;
                resMsg.Message = strb.ToString();
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [ViewPage]
        public ActionResult labellist()
        {

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var querylist = dbcontext.ADZ_LabelConfig.OrderBy(t => t.sort_id)
                .ToList();


            PagedList<ADZ_LabelConfig> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<ADZ_LabelConfig>(querylist, 1, 100, querylist.Count);
            return View(pagedlist);
        }

        [ViewPage]
        public ActionResult labelinfo(int id)
        {

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var model = dbcontext.ADZ_LabelConfig.FirstOrDefault(t => t.label_type == id);
            return View(model);
        }

        [HttpPost]
        public ActionResult labelinfo(ADZ_LabelConfig aDZ_LabelConfig)
        {
            ResponseMsg resMsg = new ResponseMsg();
            string strOutPut = "修改成功";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var model = dbcontext.ADZ_LabelConfig.FirstOrDefault(t => t.label_type == aDZ_LabelConfig.label_type);
            model.label_name = aDZ_LabelConfig.label_name;
            model.is_show = aDZ_LabelConfig.is_show;
            if (model.label_type == (int)LabelType.Huodong)
                model.is_show = 1;
            dbcontext.SaveChanges();
            resMsg.IsSuccess = true;
            resMsg.Message = strOutPut;
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="sort_type">-1 下移  1 上移</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateLabelSort(int label_type, int sort_type)
        {
            ResponseMsg resMsg = new ResponseMsg();
            string strOutPut = "移动成功";

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var now_label = dbcontext.ADZ_LabelConfig.FirstOrDefault(t => t.label_type == label_type);
            if (sort_type < 0)
            {
                var balelConfigList = dbcontext.ADZ_LabelConfig.Where(t => t.sort_id > now_label.sort_id).OrderBy(t => t.sort_id)
                    .Take(2).ToList();
                if (balelConfigList.Any())
                {
                    if (balelConfigList.Count() == 1)
                    {
                        now_label.sort_id = balelConfigList[0].sort_id + 1m;
                    }
                    if (balelConfigList.Count() == 2)
                    {
                        now_label.sort_id = (balelConfigList[0].sort_id + balelConfigList[1].sort_id) / 2;
                    }
                }
            }
            if (sort_type > 0)
            {
                var balelConfigList = dbcontext.ADZ_LabelConfig.Where(t => t.sort_id < now_label.sort_id).OrderByDescending(t => t.sort_id)
                    .Take(2).ToList();
                if (balelConfigList.Any())
                {
                    if (balelConfigList.Count() == 1)
                    {
                        now_label.sort_id = balelConfigList[0].sort_id / 2;
                    }
                    if (balelConfigList.Count() == 2)
                    {
                        now_label.sort_id = (balelConfigList[0].sort_id + balelConfigList[1].sort_id) / 2;
                    }
                }
            }
            dbcontext.SaveChanges();
            resMsg.IsSuccess = true;
            resMsg.Message = strOutPut;
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
    }
}